<?php
/**
 * Created by PhpStorm.
 * User: zy-l
 * Date: 2019/6/19
 * Time: 1:23 AM
 */

namespace App\HttpController\Api;

use App\Common\Exception\ThrowException;
use App\HttpController\Base;
use App\Model\InfoMessage;
use App\Model\InfoSetting;

/**
 * 联系管理
 * Class Contactus
 * @package App\HttpController\Api
 */
class Contactus extends Base
{

    /***
     * 联系我们设置模型
     * @return InfoSetting
     */
    public function getInfoSettingModel()
    {
        return new InfoSetting();
    }

    /**
     * @return InfoMessage
     */
    public function getInfoMessageModel()
    {
        return new InfoMessage();
    }

    /**
     * 获取配置所有数据
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function setting(){
      return $this->writeJson('获取数据列表',$this->getInfoSettingModel()->getAll([]));
    }

    /**
     * 数据添加
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function message(){
        //这边应该加个验证--每多少分钟提交一次，避免接口被刷
        if($this->isPost()){
            if(empty($this->postParam('name'))){
                throw  new ThrowException(ThrowException::ERROR_VALIDATION);
            }
            return $this->writeJson('数据添加成功',$this->getInfoMessageModel()->updateOrCreateData($this->postParams()));
        }else{
            throw  new ThrowException(ThrowException::ERROR_ACCESS_NOT_ENABLE);
        }

    }
}